// Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

namespace SSISAccess
{
    internal class ConfigDCOM
    {
        private const string DCOMKeyName = @"SOFTWARE\Microsoft\Ole";
        private const string EnableValueName = "EnableDCOM";

        public static void Enable(bool enable)
        {
            RegistryKey dcomKey = null;
            try
            {
                dcomKey = Registry.LocalMachine.OpenSubKey(DCOMKeyName, true);
                dcomKey.SetValue(EnableValueName, enable ? 'Y' : 'N');
            }
            finally
            {
                if (dcomKey != null)
                {
                    dcomKey.Close();
                }
            }
        }

        public static bool IsEnabled()
        {
            bool retVal = false;
            RegistryKey dcomKey = null;
            try
            {
                dcomKey = Registry.LocalMachine.OpenSubKey(DCOMKeyName, false);
                string enabled = (string)dcomKey.GetValue(EnableValueName);
                retVal = char.ToUpper(enabled[0]) == 'Y';
            }
            finally
            {
                if (dcomKey != null)
                {
                    dcomKey.Close();
                }
            }

            return retVal;
        }
    }
}
